class ListNavigator:
    def __init__(self, items, limitNum):
        self.items = items
        self.totalNum = len(items)
        self.selectedItemIdx = 0  # 默认选中第一个
        self.curIdx = 0  # 当前显示的开始索引
        self.limitNum = limitNum  # 每次显示的条目数

    def display(self):
        """显示当前界面，带有选中的项"""
        start_idx = self.curIdx
        end_idx = min(self.curIdx + self.limitNum, self.totalNum)

        # 显示当前可视区域内容，并标出选中项
        for idx in range(start_idx, end_idx):
            if idx == self.selectedItemIdx:
                print(f"> {self.items[idx]} <")  # 选中项用 > < 包裹
            else:
                print(self.items[idx])

    def up(self):
        """向上移动选中的项"""
        if self.selectedItemIdx > 0:
            self.selectedItemIdx -= 1
        if self.selectedItemIdx < self.curIdx:
            self.curIdx = self.selectedItemIdx

    def down(self):
        """向下移动选中的项"""
        if self.selectedItemIdx < self.totalNum - 1:
            self.selectedItemIdx += 1
        if self.selectedItemIdx >= self.curIdx + self.limitNum:
            self.curIdx += 1

    def run(self):
        """模拟导航"""
        while True:
            self.display()
            action = input("Enter 'u' to move up, 'd' to move down, or 'q' to quit: ").strip().lower()
            if action == "u":
                self.up()
            elif action == "d":
                self.down()
            elif action == "q":
                break
            else:
                print("Invalid input. Please enter 'up', 'down', or 'exit'.")
            print("\n" + "-"*20 + "\n")

# 示例
items = ["Item " + str(i) for i in range(1, 10)]  # 创建一个包含20项的列表
navigator = ListNavigator(items, limitNum=4)  # 设置每次显示5项
navigator.run()
